//
//  PPTabViewController.m
//  PPWeibo
//
//  Created by Dreamsoul on 14-11-23.
//  Copyright (c) 2014年 杭州电子科技大学. All rights reserved.
//

#import "PPTabBarController.h"
#import "HomeViewController.h"
#import "MessageViewController.h"
#import "PPTabBar.h"
#import "IWNavigationController.h"
#import "MeViewController.h"
@interface PPTabBarController ()<PPTabBarDelegate>
@property (nonatomic,weak)PPTabBar *customTabBar;
@property (nonatomic,strong)HomeViewController *home;
@property (nonatomic,strong)MessageViewController *message;
@property (nonatomic,strong)MeViewController *me;
@end

@implementation PPTabBarController
- (void)viewDidLoad{
    [super viewDidLoad];
    //自定义tabBar
    [self setupTabBar];
   //添加控制器
    [self setupAllChildController];
    //检查未读信息
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(checkUnreadCount) userInfo:nil repeats:YES];
}
- (void)checkUnreadCount{
    self.message.tabBarItem.badgeValue = @"6";
    self.me.tabBarItem.badgeValue = @"2";
}
- (void)setupAllChildController{
    HomeViewController *home = [[HomeViewController alloc] init];
    [self subVCWithController:home image:@"tabbar_home" selectedImage:@"tabbar_home_selected" title:@"首页"];
    self.home = home;
    MessageViewController *message = [[MessageViewController alloc] init];
    [self subVCWithController:message image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected" title:@"消息"];
    self.message = message;
    MeViewController *me = [[MeViewController alloc] init];
    [self subVCWithController:me image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected" title:@"个人中心"];
    self.me = me;
}
- (void)setupTabBar{
     PPTabBar *customTabBar = [[PPTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar =customTabBar;
    self.customTabBar.delegate = self;

}
- (void)tabBar:(PPTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to{
   self.selectedIndex=to;
}
//移除子控件
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}
- (void)subVCWithController:(UITableViewController *)vc image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title{
    vc.tabBarItem.image = [UIImage imageWithName:image];
    if (iOS7) {
        vc.tabBarItem.selectedImage = [[UIImage imageWithName:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }else{
        vc.tabBarItem.selectedImage = [UIImage imageWithName:selectedImage];
    }
    
    vc.title = title;
    IWNavigationController *Nvc = [[IWNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:Nvc];
    [self.customTabBar tabBarButtonWithItem:vc.tabBarItem];
}
@end
